package com.sixuerain.heartkey.db.helper;

import com.sixuerain.heartkey.db.table.HomeworkTable;
import com.sixuerain.heartkey.db.table.LectionTable;
import com.sixuerain.heartkey.db.table.ReaderRecordTable;
import com.sixuerain.heartkey.db.table.UserTable;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


/**
 * SQLiteOpenHelper
 * 
 */
public  class DatabaseHelper extends SQLiteOpenHelper {
	private static final String TAG = "DatabaseHelper";
	
	private static final String DATABASE_NAME = "HeartKey_db";
	private static final int DATABASE_VERSION = 1;
	private static DatabaseHelper instance = null;
	
	

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	public DatabaseHelper(Context context,String dbName) {
		super(context,dbName,null,DATABASE_VERSION);
	}

	public DatabaseHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
	}
	
	

	public static synchronized DatabaseHelper getInstance(Context context) {
		if (null == instance) {
			instance =  new DatabaseHelper(context);
		}
		return instance;
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.d(TAG, "Create Database.");
		db.execSQL(LectionTable.CREATE_TABLE);
		db.execSQL(HomeworkTable.CREATE_TABLE);
		db.execSQL(UserTable.CREATE_TABLE);
		db.execSQL(ReaderRecordTable.CREATE_TABLE);
		
	}

	@Override
	public synchronized void close() {
		Log.d(TAG, "Close Database.");
		super.close();
	}

	@Override
	public void onOpen(SQLiteDatabase db) {
		Log.d(TAG, "Open Database.");
		super.onOpen(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d(TAG, "Upgrade Database.");
		dropAllTables(db);
	}

	private void dropAllTables(SQLiteDatabase db) {
	}
}